package io.gitee.zhucan123.core.infrastructure.exception.advice;

import com.niimbot.middlend.core.infrastructure.exception.EntityExistsException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: zhuCan
 * @date: 2019-06-15 14:56
 * @description: 实体名称重复异常
 */
@RestControllerAdvice
@Slf4j
public class EntityExistsExceptionAdvice {

  @ExceptionHandler(EntityExistsException.class)
  @ResponseStatus(HttpStatus.CONFLICT)
  public ErrorDTO handleBindException(HttpServletRequest req, EntityExistsException ex) {
    String errorMessage = ex.getMessage();
    String errorURL = req.getRequestURL().toString();
    return new ErrorDTO(errorMessage, HttpStatus.CONFLICT.value(), errorURL);
  }
}
